package com.ding.custommanagersys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ding.custommanagersys.bo.AreaBo;
import com.ding.custommanagersys.entity.Area;
import com.ding.custommanagersys.entity.Member;
import com.ding.custommanagersys.entity.ResultData;
import com.ding.custommanagersys.service.IMemberService;
import com.ding.custommanagersys.service.impl.MemberServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ding
 * @since 2023-04-18
 */
@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private IMemberService memberService;

    /**
     * 分页显示 会员信息
     */
    @RequestMapping("/listAllMember")
    public ResultData<List<Member>> listAllMember(AreaBo bo){
        ResultData<List<Member>> resp = new ResultData<>();
        Page<Member> page = memberService.listAllMember(bo);
        resp.setCount(page.getTotal());
        resp.setData(page.getRecords());
        return resp.ok();
    }

    /**
     * 添加 会员信息
     */
    @PostMapping("/addMember")
    public ResultData<Integer> addMember(@RequestBody Member member){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = memberService.addMember(member);
        if(num == 0){
            resp.error("添加失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }

    /**
     * 删除 会员信息
     */
    @PostMapping("/deleteMember")
    public ResultData<Integer> deleteMember(@RequestBody AreaBo bo){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = memberService.deleteMember(bo.getIds());
        if(num != 0){
            resp.ok();
        }else{
            resp.error("该编号下没有会员信息，或编号为空");
        }
        return resp;
    }

    /**
     * 修改 修改会员
     */
    @PostMapping("/updateMember")
    public ResultData<Integer> updateArea(Member member){
        ResultData<Integer> resp = new ResultData<>();
        Integer num = memberService.updateMember(member);
        if(num == 0){
            resp.error("修改失败，请联系管理员！");
        }else{
            resp.ok();
        }
        return resp;
    }

}
